class Solution:
    def trap(self, height):
        length = len(height)
        i, j, res = 0, length - 1, 0
        max_side = min(height[i], height[j])
        while True:
            directin = (height[i] < height[j])
            if directin:
                i += 1
                if height[i] < max_side:
                    res += (max_side - height[i])
                else:
                    max_side = min(height[i], height[j])
            else:
                j -= 1
                if height[j] < max_side:
                    res += (max_side - height[j])
                else:
                    max_side = min(height[i], height[j])
            if j - i == 1:
                break
        return res


a = Solution()
print(a.trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]))
